﻿Imports System.Data
Imports System.Data.SqlClient
Imports Entidad

Public Class DocumentoAlmacenPickeadorTiempoDatos

    Sub ingresarDocumentoAlmacenPickeadorTiempo(ByVal dapt As DocumentoAlmacenPickeadorTiempo, flag As String)

        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "INSERT INTO Documento_Almacen_Pickeador_Tiempo(DAP_Codigo,CIA, " &
                              "SUC_Codigo,ALM_Codigo,TDOC_Codigo,DCA_Codigo,PICK_Codigo, " &
                              "EVE_Codigo,DAPT_FechaIni,DAPT_FechaFin,DAPT_Activo) " &
                              "values(@dapCodigo,@cia,@suc,@alm,@tipo,@dalCodigo,@pickCodigo, " &
                              "@eveCodigo," & IIf(flag = "1", "'" & dapt.FECHA_ASIG & "',", "GETDATE(),") & IIf(flag = "1", "GETDATE()", "NULL") & ",'1')"

        Dim comando As New SqlCommand
        With comando
            .Connection = coneccion
            .CommandText = query
            .Parameters.AddWithValue("@dapCodigo", dapt.DAP_Codigo)
            .Parameters.AddWithValue("@cia", dapt.CIA)
            .Parameters.AddWithValue("@suc", dapt.SUC_Codigo)
            .Parameters.AddWithValue("@alm", dapt.ALM_Codigo)
            .Parameters.AddWithValue("@tipo", dapt.TDOC_Codigo)
            .Parameters.AddWithValue("@dalCodigo", dapt.DAL_Codigo)
            .Parameters.AddWithValue("@pickCodigo", dapt.PICK_Codigo)
            .Parameters.AddWithValue("@eveCodigo", dapt.EVE_Codigo)
            .ExecuteNonQuery()
        End With

        coneccion.Close()

    End Sub

    Function obtenerCodigo(ByVal codPedido As String, ByVal codPick As String, tipoDOC As String) As Integer
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "SELECT TOP 1 DAP_Codigo " &
                              "FROM Documento_Almacen_Pickeador_Tiempo " &
                              "WHERE DCA_Codigo = '" & codPedido & "' " &
                              "AND TDOC_Codigo = '" & tipoDOC & "' " &
                              "AND PICK_Codigo = '" & codPick & "' " &
                              "AND EVE_Codigo IN ('01','07') " &
                              "ORDER BY DAPT_Codigo DESC"

        Dim adapter As New SqlDataAdapter(query, coneccion)
        Dim ds As New DataSet
        adapter.Fill(ds)

        coneccion.Close()

        Return CInt(ds.Tables(0).Rows(0)("DAP_Codigo").ToString.Trim)
    End Function

    Function verificarDocumentoAlmacenPickeadorTiempo(ByVal codPed As String, ByVal codpick As String, ByVal ppCod As String, tipoDOC As String) As String
        Dim codigoEve As String = ""
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "SELECT TOP 1 EVE_Codigo " &
                              "FROM Documento_Almacen_Pickeador_Tiempo " &
                              "WHERE DCA_Codigo = '" & codPed & "' " &
                              "AND TDOC_Codigo = '" & tipoDOC & "' " &
                              "AND PICK_Codigo = '" & codpick & "' " &
                              "AND DAP_Codigo = '" & ppCod & "' " &
                              "ORDER BY DAPT_Codigo DESC"

        Dim adapter As New SqlDataAdapter(query, coneccion)
        Dim ds As New DataSet
        adapter.Fill(ds)

        coneccion.Close()

        If ds.Tables(0).Rows.Count <> 0 Then
            codigoEve = ds.Tables(0).Rows(0)("EVE_Codigo").ToString.Trim
        End If

        Return codigoEve
    End Function

    Function verificacionActivacionPedido(cia As String, suc As String, alm As String, tdoc As String, dcicodigo As String, codPickeador As String) As Boolean
        Dim valor As Boolean = False
        Dim conex As New Conexion
        Dim coneccion As SqlConnection = conex.conectar()
        coneccion.Open()
        Dim query As String = "SELECT EVE_Codigo " &
                              "FROM Documento_Almacen_Pickeador_Tiempo " &
                              "WHERE CIA = '" & cia & "' AND SUC_Codigo = '" & suc & "' " &
                              "AND ALM_Codigo = '" & alm & "' AND TDOC_Codigo = '" & tdoc & "' " &
                              "AND DCA_Codigo = '" & dcicodigo & "' " &
                              "AND PICK_Codigo = '" & codPickeador & "' " &
                              "AND EVE_Codigo = '05' " &
                              "ORDER BY DAPT_Codigo DESC"

        Dim adapter As New SqlDataAdapter(query, coneccion)
        Dim ds As New DataSet
        adapter.Fill(ds)

        coneccion.Close()

        If ds.Tables(0).Rows.Count > 0 Then
            valor = True
        End If

        Return valor
    End Function

End Class
